home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / DSPanic / GrafPanic.c next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  3.9 KB  |  166 lines

  1. /*
  2.  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
  3.  *
  4.  * @APPLE_LICENSE_HEADER_START@
  5.  *
  6.  * The contents of this file constitute Original Code as defined in and
  7.  * are subject to the Apple Public Source License Version 1.1 (the
  8.  * "License").  You may not use this file except in compliance with the
  9.  * License.  Please obtain a copy of the License at
  10.  * http://www.apple.com/publicsource and read it before using this file.
  11.  *
  12.  * This Original Code and all software distributed under the License are
  13.  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  14.  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  16.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
  17.  * License for the specific language governing rights and limitations
  18.  * under the License.
  19.  *
  20.  * @APPLE_LICENSE_HEADER_END@
  21.  */
  22.  /*
  23.   * Portions Copyright (c) 2001 Louis Gerbarg. All rights reserved
  24.   *
  25.   * This is evil nasty unreliable code. Do not use this. 
  26.   * Why are you still reading this !?!
  27.   
  28.   
  29.   
  30.   
  31.   
  32.   
  33.   
  34.   
  35.   
  36.   
  37.   
  38.   
  39.   
  40.   
  41.   
  42.   
  43.   
  44.   
  45.   
  46.   
  47.   
  48.   
  49.   
  50.   
  51.   
  52.   * Fine, I am not going to win. You have been warned. Don't come to me when your system
  53.   * turns into a steaming pile of melted poly-carbonate.
  54.   */
  55.  
  56. #include <mach/mach_types.h>
  57.  
  58. #include "PatchIt.h" 
  59. #include <UserNotification/KUNCUserNotifications.h>
  60. #include <kern/lock.h>
  61. #include <kern/task.h>
  62. #include <kern/thread.h>
  63. #include <stdarg.h>
  64. #include <string.h>
  65. //#include <kern/printf.h>
  66. #include <sys/malloc.h>
  67.  
  68.  
  69. extern int return_on_panic;
  70.  
  71. extern void
  72. panic(const char *str, ...);
  73.  
  74. void
  75. my_panic(PatchPtr patch, ...);
  76.  
  77. static char *display_string;
  78. static char *display_string_internal;
  79.  
  80. kern_return_t DSPanic_start (kmod_info_t * ki, void * d)
  81. {
  82.     InitPatchIt();
  83.     PatchIt(panic, my_panic);
  84.     
  85.     //Oh boy, lets waste 8k of kernel ram cause I am lazy.
  86.     
  87.     MALLOC(display_string, char *,PAGE_SIZE, M_TEMP, M_WAITOK);
  88.     MALLOC(display_string_internal, char *,PAGE_SIZE, M_TEMP, M_WAITOK);
  89.     
  90.     //Vestigial, but just in case....
  91.     
  92.     return_on_panic = 1;
  93.     
  94.     //Sanity checking, I would only need that if I were sane.
  95.     
  96.     return KERN_SUCCESS;
  97. }
  98.  
  99. kern_return_t DSPanic_stop (kmod_info_t * ki, void * d)
  100. {
  101.     return_on_panic = 0;
  102.     
  103.     //I suppose I should free that...
  104.     
  105.     FREE(display_string, M_TEMP);
  106.     FREE(display_string_internal, M_TEMP);
  107.  
  108.  
  109.  
  110.     UnInitPatchIt();
  111.     
  112.     //I was not sane then, and I am not sane now.
  113.     
  114.     return KERN_SUCCESS;
  115. }
  116.  
  117. //stolen blantantly from the kernel sources.
  118.  
  119. static char *paniccopy_str;
  120.  
  121. static void
  122. paniccopybyte(
  123.         char byte)
  124. {
  125.   *paniccopy_str++ = byte;
  126.   *paniccopy_str = '\0';
  127. }
  128.  
  129. //I have no idea what header this is in, and I am not looking at this hour.
  130.  
  131. extern void
  132. _doprnt(register const char *fmt, va_list *argp,void (*putc)(char),int radix);
  133.  
  134. extern mutex_t sprintf_lock;
  135.  
  136. //This is the good part.
  137.  
  138. void my_panic(PatchPtr patch, ...)
  139. {
  140.  
  141.     va_list listp;
  142.     char *fmt;
  143.     
  144.     fmt = (char *)patch->arg0Save;
  145.     strcpy(display_string_internal, "The kernel \"xnu\" has unexpectedly panicked with error \"\0");
  146.     strcat(display_string_internal, fmt);
  147.     strcat(display_string_internal, ".\" Please save all active work and restart the machine.\0");
  148.  
  149.     va_start(listp, patch);
  150.     paniccopy_str = display_string;
  151.     _doprnt(display_string_internal, &listp, paniccopybyte, 16);
  152.     va_end(listp); 
  153.  
  154.     /* Now I am going to trigger a userspace code path after the panic
  155.      * There are about 12 bazillion things that can go wrong here.
  156.      * And that doesn't include if any of the code path is paged out ;-)
  157.      */
  158.  
  159.     KUNCUserNotificationDisplayNotice( 600, 0, 0, 0, 0, 
  160.         "DSPanic", display_string , "Okay");
  161.  
  162.     /* I should call through to panic, but it has problems.
  163.      * ...
  164.      * Oh, and I would need to forge the stack frame. Fuck it, I am done.
  165.      */
  166. }